<template>
  <el-dialog
    title="提示"
    :visible="dialogVisible"
    width="40%"
    @close="onClose"
  >
    <el-tree
      ref="tree"
      :data="data"
      show-checkbox
      default-expand-all
      node-key="id"
      highlight-current
      :props="defaultProps"
    />
    <span slot="footer" class="dialog-footer">
      <el-button @click="onClose">取 消</el-button>
      <el-button type="primary" @click="editUserRights">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { authorityList } from '@/api/authority'
import { editAuthority } from '@/api/roles'
export default {
  name: 'EditRight',
  model: {
    prop: 'dialogVisible'
  },
  props: {
    dialogVisible: {
      type: Boolean,
      required: true
    }
  },
  data() {
    return {
      defaultProps: {
        children: 'children',
        label: 'authName'
      },
      data: [],
      roleId: ''
    }
  },
  created() {
  },
  methods: {
    async loadTreeData() {
      this.data = await authorityList('tree')
    },
    setSelect(data) {
      this.$refs.tree.setCheckedNodes(data)
    },
    async editUserRights() {
      const rids = this.$refs.tree.getCheckedKeys()
      await editAuthority(this.roleId, rids.join(','))
      this.$message.success('分配权限完成')
      this.$emit('success')
      this.onClose()
    },
    onClose() {
      this.$emit('input', false)
    }
  }
}
</script>

<style scoped>

</style>
